#pragma once
/*
 * @Author: qianlihaoyue
 * @Date: 2023-05-11 17:07
 * @LastEditTime: 2023-06-22 17:23
 * @Description:
 * @Version: V1.0
 *
 */

 /*
 PclViewer PclView(2, 1);

 PclView.show();
 */

#include <pcl/visualization/pcl_visualizer.h>

template<typename PointType>
class PclViewer {
    using CloudType = typename pcl::PointCloud<PointType>;
    using CloudPtr = typename pcl::PointCloud<PointType>::Ptr;

public:
    PclViewer(int c_num, int r_num) : c_num_(c_num), r_num_(r_num), viewer("PCL Viewer") {
        factor_[0] = 1.0f / c_num;
        factor_[1] = 1.0f / r_num;
        viewer.setBackgroundColor(0, 0, 0);
        // 视窗ID是自动生成的，按顺序递增，从1开始
        for (int i = 0; i < c_num_ * r_num_; ++i) {
            createSubPlot(i % c_num_, i / c_num_);
        }
    }

    // 默认title subplot+id
    void addTitle(int view_id, std::string title_name) {
        viewer.addText(title_name, 60, 10, 20, 1, 1, 1, "title" + std::to_string(view_id), view_id);
    }

    void addMesh(pcl::PolygonMesh mesh, std::string mesh_name, int view_id, std::vector<int> rgb = { 255,255,255 }, bool addTitle = false) {
        if (addTitle) viewer.addText(mesh_name, 50, 10, 20, 1, 1, 1, "title" + std::to_string(view_id), view_id);
        viewer.addPolygonMesh(mesh, mesh_name + std::to_string(view_id), view_id);
        viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, rgb[0], rgb[1], rgb[2], mesh_name + std::to_string(view_id));
    }

    // void addMesh(pcl::PolygonMesh mesh, std::string mesh_name,int col,int row, std::vector<int> rgb={255,255,255}) {
    // 	addMesh(mesh, mesh_name, row * c_num_ + col, rgb);
    // }

    void addCloud(CloudPtr cloud, std::string cloud_name, int view_id, std::vector<int> rgb = { 255,255,255 }, bool addTitle = false) {
        if (addTitle) viewer.addText(cloud_name, 50, 10, 20, 1, 1, 1, "title" + std::to_string(view_id), view_id);
        pcl::visualization::PointCloudColorHandlerCustom<PointType> cloud_color(cloud, rgb[0], rgb[1], rgb[2]);
        viewer.addPointCloud<PointType>(cloud, cloud_color, cloud_name + std::to_string(view_id), view_id);
    }

    // void addCloud(CloudPtr cloud, std::string cloud_name,int col,int row, std::vector<int> rgb={255,255,255}) {
    // 	addCloud(cloud, cloud_name, row * c_num_ + col, rgb);
    // }


    void show() {
        viewer.initCameraParameters();
        viewer.addCoordinateSystem(1.0);
        viewer.spin();
    }

    pcl::visualization::PCLVisualizer viewer;

private:
    int createSubPlot(int col, int row) {
        if (col >= c_num_ || row >= r_num_) {
            PCL_ERROR("Invalid subplot position\n");
            return -1;
        }
        // 视窗ID是自动生成的，按顺序递增，从1开始
        // int view_id = row * c_num_ + col;
        int view_id;
        viewer.createViewPort(factor_[0] * col, factor_[1] * row, factor_[0] * (col + 1), factor_[1] * (row + 1), view_id);
        viewer.addText("ID " + std::to_string(view_id), 10, 10, 20, 1, 1, 1, "id" + std::to_string(view_id), view_id);
        return view_id;
    }

    int c_num_ = 1;
    int r_num_ = 1;
    float factor_[2];
};


